package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    /**
     * 平台用户列表界面查询
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model){
        return "region/list";
    }
    @RequestMapping("/selectTreeViewByParentId")
    @ResponseBody
    public List selectTreeViewByParentId(Long parentId,String type){
        List<Region> regions = regionService.listAll(parentId);
        //判断是否获取treeview需要的值,还是原生数据
        if ("tree".equals(type)){
            List<Map> list = new ArrayList();
            //封装数据用的数组
            List<Map> result = new ArrayList<>();
            for (Region region : regions) {
                //调用方法,封装数据
                Map<String, Object> treeView = region.getTreeView(region);
                list.add(treeView);
            }
            //调用treeview方法将数据封装成所需要的数据
            return list;
        }else {//返回原生数据
            return regions;
        }
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Region region){
        JSONResult jsonResult = new JSONResult();
        regionService.saveOrUpdate(region);
        return jsonResult;
    }
    @RequestMapping("/changeState")
    @ResponseBody
    public JSONResult changeState(Long id,Integer state){
        JSONResult jsonResult = new JSONResult();
        regionService.changeState(id,state);
        return jsonResult;
    }
}
